﻿using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace System.Composition
{
    //public interface ISatisfyImportsOnce
    //{
    //}

    //public interface ISatisfyImportsOnceMetadata
    //{
    //    Type AutoComposableObjectType { get; }
    //}

    //[MetadataAttribute]
    //[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    //public class AutoComposeOnceAttribute : ExportAttribute, ISatisfyImportsOnceMetadata
    //{
    //    public Type AutoComposableObjectType { get; private set; }
    //    public AutoComposeOnceAttribute(Type AutoComposableType)
    //        : base(typeof(ISatisfyImportsOnce))
    //    {
    //        AutoComposableObjectType = AutoComposableType;
    //    }
    //}

    //public static class ISatisfyImportsExtensions
    //{
    //    public static void SatisfyImportsOnce(this ISatisfyImportsOnce self)
    //    {
    //        ICompositionService svc = (Application.Current as ICompositionService);
    //        if (svc != null)
    //            svc.SatisfyImportsOnce(self);
    //    }
    //}

    //public static class CompositionContainerExtensions
    //{
    //    public static void SatisfyImportsOnce(this CompositionService Container)
    //    {
    //        var importonce = Container.GetExports(typeof(ISatisfyImportsOnce), typeof(ISatisfyImportsOnceMetadata), string.Empty);
    //        if (importonce != null && importonce.Count() > 0)
    //            importonce
    //                .Where(lazyObject => lazyObject.Metadata != null)
    //                .Where(lazyObject => lazyObject.Metadata is ISatisfyImportsOnceMetadata)
    //                .Where(lazyObject =>
    //                {
    //                    Type objType = (lazyObject.Metadata as ISatisfyImportsOnceMetadata).AutoComposableObjectType;
    //                    var interfaces = objType.GetInterfaces();
    //                    return !interfaces.Any((t) => t.FullName.Contains("UIElement"));
    //                })
    //                .ForEach((lazyObject) =>
    //                {
    //                    var meta = lazyObject.Metadata as ISatisfyImportsOnceMetadata;
    //                    if (meta.AutoComposableObjectType != null && !meta.AutoComposableObjectType.IsSubclassOf(typeof(UIElement)))
    //                        Container.SatisfyImportsOnce(lazyObject.Value);
    //                });
    //    }
    //}

}
